<?php
namespace plugins\pay\controller;
use cmf\controller\PluginAdminBaseController;
use plugins\pay\lib\Pay;
use plugins\pay\lib\Common as LCOM;

class AdminIndexController extends PluginAdminBaseController
{
    public function index()
    {
        $controller = '\\plugins\\pay\\controller';
        
        $plugins = $this->getPlugins();
        
        $payListConfig = [];
        $paylist = [];
        
        $switchOn = LCOM::getPayListWithOn();
        foreach ( $plugins as $key => $value ){
            $class  = $controller.'\\'.cmf_parse_name( $value , 1 ).'Controller';
            if( class_exists( $class ) ){
                $obj    = new $class; 
                if( method_exists( $obj , 'config' ) ){
                    $html = $obj->config( '<include file="'.strtolower( $value ).'/config" />' );
                    $payListConfig [ $value ] [ 'title' ] = isset( $obj->title ) ? $obj->title : $value;
                    $payListConfig [ $value ] [ 'html' ]  = $html;
                    $tmp = [
                        'value' => $value,
                        'title' => $payListConfig [ $value ] [ 'title' ],
                        'checked'  => in_array( $value , $switchOn ) ? 'checked' : '',
                        'tx' => in_array( $value , $switchOn ) ? '功能已开启' : '功能已关闭' 
                    ];
                    $paylist [] = $tmp;
                }
            }
            
        }
        $this->assign( 'payList' , $paylist );
        $this->assign( 'payListConfig' , $payListConfig );
        return $this->fetch();
    }
    
    //获取插件
    private  function getPlugins()
    {
        $path = PLUGINS_PATH.'pay'.DS.'lib';
        $dir  = scandir( $path );
        $list = [];
        foreach ( $dir as $key => $value ){
            if( $value == '.' || $value == '..' ) continue;
            if( is_dir ( $path . DS . $value ) ) $list [] = $value;
        }
        return $list;
    }
    
    //上传支付方式的图标
    public function payIcon()
    {
        $data = $this->request->param();
        if( empty( $_FILES ) ){
            return api_json_echo( false , '未获取到数据' );
        }
        $path = DS.'plugins'.DS.'pay'.DS.'config';
        $rule = [ 
            'size' => 1024*200,    //200kb
            'ext' =>['jpeg' , 'png' , 'jpg' ],
            'type' => [ 'image/jpeg' , 'image/png' , 'image/jpg' ]
        ];
        
        $res = uploadImg( $_FILES [ 'file' ] , $path , $rule );
        
        if( !$res[ 'status' ] ){
            return api_json_echo( false , '上传失败' , $res [ 'message' ] , $res [ 'code' ] );
        }
        
        return api_json_echo( true , '上传成功' , $res [ 'data' ] );
    }
    
    
    //保存开启的列表
    public function savePayList()
    {
        $data = $this->request->param();
        $list = !isset( $data [ 'list' ] ) || empty( $data [ 'list' ] ) ? [] : array_keys( $data [ 'list' ] );
        LCOM::savePayListWithOn( json_encode( $list ) );
        return api_json_echo( true , '保存成功' );
    }
    
    
    
    
}